package org.go.scheduler.database;

import org.go.expcetion.SchedulerConfigException;
import org.go.expcetion.SchedulerException;
import org.go.scheduler.Scheduler;
import org.go.spi.TriggerFiredBundle;
import org.go.util.ClassUtils;
import org.go.work.WorkRunShell;

/**
 * <p>
 * Responsible for creating the instances of a {@link JobRunShell}
 * to be used within the <class>{@link org.quartz.core.QuartzScheduler}
 * </code> instance.  It will create a standard {@link JobRunShell}
 * unless the job class has the {@link ExecuteInJTATransaction}
 * annotation in which case it will create a {@link JTAJobRunShell}.
 * </p>
 * 
 * <p>
 * This implementation does not re-use any objects, it simply makes a new
 * JTAJobRunShell each time <code>borrowJobRunShell()</code> is called.
 * </p>
 * 
 * @author James House
 */
public class JTAAnnotationAwareJobRunShellFactory implements JobRunShellFactory {

	private Scheduler scheduler;

	public JTAAnnotationAwareJobRunShellFactory() {
	}

	/**
	 * <p>
	 * Called by the <class>{@link org.quartz.core.QuartzSchedulerThread}
	 * </code> to obtain instances of <code>
	 * {@link org.quartz.core.JobRunShell}</code>.
	 * </p>
	 */
	@Override
	public WorkRunShell createJobRunShell(TriggerFiredBundle bundle) throws SchedulerException {
		boolean needsJTA = ClassUtils.isAnnotationPresent(bundle.getJobDetail().getWorkClass(), ExecuteInJTATransaction.class);

		if (needsJTA) {
		}
		//return new JTAJobRunShell(scheduler, bundle);
		else {
		}
		//return new WorkerRunShell(scheduler, bundle);
		return null;
	}

	/**
	 * <p>
	 * Initialize the factory, providing a handle to the <code>Scheduler</code>
	 * that should be made available within the <code>JobRunShell</code> and
	 * the <code>JobExecutionContext</code> s within it, and a handle to the
	 * <code>SchedulingContext</code> that the shell will use in its own
	 * operations with the <code>JobStore</code>.
	 * </p>
	 */
	@Override
	public void initialize(Scheduler scheduler) throws SchedulerConfigException {
		this.scheduler = scheduler;
	}
}
